package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping
    public Result addDisk(@RequestBody DishDTO dishDTO) {
        dishService.add(dishDTO);
        extracted("dish_" + dishDTO.getCategoryId());
        return Result.success();
    }

    @GetMapping("/page")
    public Result pageDish(DishPageQueryDTO queryDTO) {
        PageResult pageResult = dishService.pageDish(queryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        dishService.deleteByids(ids);
        extracted("*dish_*");
        return Result.success();
    }

    @PutMapping
    public Result updateInfo(@RequestBody DishDTO dishDTO) {
        dishService.update(dishDTO);
        extracted("*dish_*");
        return Result.success();
    }

    private void extracted(String pattern) {
        System.out.println("清除" + pattern);
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    @GetMapping("/{id}")
    public Result getDishByid(@PathVariable Long id) {
        DishVO byid = dishService.getByid(id);
        return Result.success(byid);
    }

    @PostMapping("/status/{status}")
    public Result UpdateStatus(@PathVariable Integer status, Integer id) {
        extracted("*dish_*");
        dishService.updateStatus(status, id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getListByCId(Long categoryId) {
        List<Dish> list = dishService.getByCid(categoryId);
        return Result.success(list);
    }

}
